package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.qcloud.QCloudAntiFraudVipV5Data;
import com.ucredit.babel.common.model.qcloud.QCloudAntiFraudVipV5Record;
import com.ucredit.babel.common.model.qcloud.QCloudAntiFraudVipV5RiskInfo;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.List;


/*
 * 腾讯云定制反欺诈分
 *
 * @author:szw
 * */

public class TencentAntifraudVipParse extends ParseBase {
    private Text key = new Text();
    private Text val = new Text();

    /**
     * 腾讯云定制反欺诈分
     *
     * @param context
     * @param model
     * @param successful
     * @return boolean
     * @throws IOException
     * @throws InterruptedException
     * @author typole
     * @date 2020/12/10 5:08 下午
     */

    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        QCloudAntiFraudVipV5Record obj = (QCloudAntiFraudVipV5Record) model;
        QCloudAntiFraudVipV5Data data = obj.getData();
        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder TxSb = new StringBuilder(500);
        StringBuilder txSb = getBaseInfo(obj);

        if (data != null) {
            // code 公公错误码
            TxSb.append(data.getCode() == null ? "" : data.getCode()).append(CRT001);
            // codeDesc 业务侧错误码
            TxSb.append(data.getCodeDesc() == null ? "" : data.getCodeDesc()).append(CRT001);
            // message 模块错误信息描述
            TxSb.append(data.getMessage() == null ? "" : data.getMessage()).append(CRT001);
            // found 该条记录能否查到
            TxSb.append(data.getFound()).append(CRT001);
            // idFound 该条记录中的身份证能否查到
            TxSb.append(data.getIdFound()).append(CRT001);
            // riskScore 欺诈分
            TxSb.append(data.getRiskScore()).append(CRT001);
            // 请求时间
            TxSb.append(data.getPostTime() == null ? "" : data.getPostTime()).append(CRT001);

            List<QCloudAntiFraudVipV5RiskInfo> infoList = data.getRiskInfo();
            if (!infoList.isEmpty()) {
                for (QCloudAntiFraudVipV5RiskInfo info : infoList) {
                    // 风险码
                    TxSb.append(info.getRiskCode()).append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }
            } else {
                // 风险码为空的时候
                TxSb.append(CRT001);
                key.set(getKey());
                val.set((sb.toString() + TxSb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
                context.write(key, val);
            }
        }

        return  data != null;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tencent_antifraud_vip_inc";
    }

    @Override
    public String getType() {
        return RecordType.QCLOUD_ANTIFRAUDVIP_V5_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "腾讯云定制反欺诈分";
    }
}
